//
//  CXHTopOnSplashAdapter.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/10.
//

#import "CXHTopOnSplashAdapter.h"
#import "CXHTopOnSplashCustomEvent.h"
#import "CXHTopOnCommonBiddingRequest.h"
#import "CXHTopOnCommonBiddingManager.h"
#if __has_include(<AnyThinkSDK/AnyThinkSDK.h>)
#import <AnyThinkSDK/AnyThinkSDK.h>
#else
#import "AnyThinkSDK.h"
#endif
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Core/CXHAdSDKSplashAd.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHTopOnSplashAdapter ()<CXHAdSDKSplashAdDelegate>

@property (nonatomic, strong) CXHAdSDKSplashAd *splashAd;
@property (nonatomic, strong) CXHTopOnSplashCustomEvent *customEvent;

@end

@implementation CXHTopOnSplashAdapter

#pragma mark - load Ad
- (void)loadADWithArgument:(ATAdMediationArgument *)argument {
    self.splashAd = [[CXHAdSDKSplashAd alloc] init];
    self.splashAd.delegate = self;
    self.splashAd.posId = argument.serverContentDic[@"posid"];
    self.splashAd.setSkipButtonToRightTop = YES;
//    UIView *containerView = localInfo[kATSplashExtraContainerViewKey];
//    self.splashAd.bottomView = containerView;
    [self.splashAd loadSplashAd];
}
 
// Ad ready
- (BOOL)adReadySplashWithInfo:(NSDictionary *)info {
    return self.splashAd.isAdValid;
}

// Ad show
- (void)showSplashAdInWindow:(UIWindow *)window inViewController:(UIViewController *)inViewController parameter:(NSDictionary *)parameter {
    [self.splashAd showSplashAdInWindow:window];
}

#pragma mark - C2S Win Loss
- (void)didReceiveBidResult:(ATBidWinLossResult *)result {
    if (result.bidResultType == ATBidWinLossResultTypeWin) {
        [self.splashAd sendWinNotice:[result.winPrice integerValue]];
        return;
    }
    [self.splashAd sendLossNotice:[result.secondPrice integerValue]];
}

#pragma mark - CXHAdSDKSplashAdDelegate
/**
 开屏广告点击回调, 可能会打开 AppStore, WebView, ThirdApp etc.

 @param splashAd 开屏广告
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_splashAdDidClick:(CXHAdSDKSplashAd *)splashAd loadingPageURL:(NSString *)loadingPageURL{
    [self.adStatusBridge atOnAdClick:nil];
}

/**
 开屏广告关闭回调, 触发条件: 点击广告, 点击跳过, 超时关闭

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidClose:(CXHAdSDKSplashAd *)splashAd{
    [self.adStatusBridge atOnAdClosed:nil];
}

/**
 开屏广告展示回调

 @param splashAd 开屏对象
 */
- (void)CXH_splashAdExposured:(CXHAdSDKSplashAd *)splashAd{
    [self.adStatusBridge atOnAdShow:nil];
}

/**
 开屏广告加载成功
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidLoad:(CXHAdSDKSplashAd *)splashAd{
    NSDictionary * extraDic = [CXHTopOnCommonAdapter getC2SInfo:[self.splashAd getBidPrice]];
    [self.adStatusBridge atOnSplashAdLoadedExtra:extraDic];
}

/**
 开屏广告加载失败
 
 @param splashAd 开屏对象
 */
- (void)CXH_splashAdDidFailToLoad:(CXHAdSDKSplashAd *)splashAd error:(NSError *)error{
    [self.adStatusBridge atOnAdLoadFailed:error adExtra:nil];
}

/**
 *  开屏广告成功展示
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdSuccessPresentScreen:(CXHAdSDKSplashAd *)splashAd{
    
}

/**
 *  开屏广告展示失败
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdFailToPresent:(CXHAdSDKSplashAd *)splashAd withError:(NSError *)error{
    [self.adStatusBridge atOnAdShowFailed:error extra:nil];
}

/**
 *  开屏广告关闭落地页
 *
 *  @param splashAd 开屏对象
 */
- (void)CXH_splashAdCloseLandingPage:(CXHAdSDKSplashAd *)splashAd{
    [self.adStatusBridge atOnAdDetailClosed:nil];
}

//- (nonnull instancetype)initWithNetworkCustomInfo:(nonnull NSDictionary *)serverInfo localInfo:(nonnull NSDictionary *)localInfo {
//    self = [super init];
//    if (self != nil) {
//        NSString *domain = [serverInfo CXH_objectOrNilForKey:@"domain"];
//        NSString *appid = [serverInfo CXH_objectOrNilForKey:@"appid"];
//        if (!kCXHStringIsEmpty(appid)) {
//            [CXHAdSDK initWithAppId:appid requestDomain:domain completionBlock:^(NSError * _Nullable error) {
//                if (error) {
//                    NSLog(@"CXHSDK 初始化失败%@",error);
//                }
//            }];
//        }
//    }
//    return self;
//}
//
//- (void)loadADWithInfo:(nonnull NSDictionary *)serverInfo localInfo:(nonnull NSDictionary *)localInfo completion:(nonnull void (^)(NSArray<NSDictionary *> * _Nonnull, NSError * _Nonnull))completion {
//    
//    
//    NSString *bidId = serverInfo[kATAdapterCustomInfoBuyeruIdKey];
//    NSString *posid = serverInfo[@"posid"];
//    NSString *requestUUID = serverInfo[@"tracking_info_request_id"];
//    if (bidId) {
//        CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingManager sharedInstance] getRequestItemWithUnitID:requestUUID];
//        if (request) {
//            self.customEvent = (CXHTopOnSplashCustomEvent *)request.customEvent;
//            self.customEvent.requestCompletionBlock = completion;
//            self.splashAd = request.customObject;
//            
//            [self.customEvent trackSplashAdLoaded:self.splashAd adExtra:nil];
//        }
//        
//        [[CXHTopOnCommonBiddingManager sharedInstance] removeRequestItemWithUnitID:requestUUID];
//        return;
//    }
//    
//    _customEvent = [[CXHTopOnSplashCustomEvent alloc] initWithInfo:serverInfo localInfo:localInfo];
//    _customEvent.requestCompletionBlock = completion;
//    dispatch_async(dispatch_get_main_queue(), ^{
//        NSLog(@"======普通加载广告====");
//        self.splashAd.delegate = self.customEvent;
//        self.splashAd.posId = serverInfo[@"posid"];
//        self.splashAd.setSkipButtonToRightTop = YES;
//        UIView *containerView = localInfo[kATSplashExtraContainerViewKey];
//        self.splashAd.bottomView = containerView;
//        [self.splashAd loadSplashAd];
//    });
//}
//
//+ (BOOL)adReadyWithCustomObject:(nonnull id)customObject info:(nonnull NSDictionary *)info {
//    CXHAdSDKSplashAd *splashAd = customObject;
//    return [splashAd isAdValid];
//}
//
//+ (BOOL)isSupportAdType:(nonnull ATUnitGroupModel *)unitGroupModel { 
//    return YES;
//}
//
//+ (void)showSplash:(ATSplash *)splash localInfo:(NSDictionary *)localInfo delegate:(id)delegate {
//    CXHAdSDKSplashAd *splashAd = splash.customObject;
//    
//    // 这里可以获取到开发者在 show ad 时传入的 window
//    UIWindow *window = localInfo[kATSplashExtraWindowKey];
//    [splashAd showSplashAdInWindow:window];
//}
//
//- (CXHAdSDKSplashAd *)splashAd{
//    if (_splashAd == nil) {
//        _splashAd = [[CXHAdSDKSplashAd alloc] init];
//    }
//    return _splashAd;
//}
//
//+ (void)bidRequestWithPlacementModel:(ATPlacementModel *)placementModel unitGroupModel:(ATUnitGroupModel *)unitGroupModel info:(NSDictionary *)info completion:(void(^)(ATBidInfo *bidInfo, NSError *error))completion{
//    NSString *domain = [info CXH_objectOrNilForKey:@"domain"];
//    NSString *appid = [info CXH_objectOrNilForKey:@"appid"];
//    if (!kCXHStringIsEmpty(appid)) {
//        [CXHTopOnSplashAdapter initCXHSDKWith:appid domain:domain completion:^{}];
//    }
//    
//    NSString *requestUUID = info[@"tracking_info_request_id"];
//    NSLog(@"=====requestuuid%@", requestUUID);
//    
//    CXHTopOnCommonBiddingRequest *request = [[CXHTopOnCommonBiddingRequest alloc] init];
//    request.unitGroup = unitGroupModel;
//    request.placementID = placementModel.placementID;
//    
//    request.bidCompletion = completion;
//    request.unitID = info[@"posid"];
//    request.extraInfo = info;
//    request.adType = CXHAdFormatSplash;
//    request.UUID = requestUUID;
//
//    
//    CXHTopOnSplashCustomEvent *customEvent = [[CXHTopOnSplashCustomEvent alloc] initWithInfo:info localInfo:info];
//    customEvent.isC2SBiding = YES;
//    customEvent.UUID = requestUUID;
//    
//    request.customEvent = customEvent;
//
//    [[CXHTopOnCommonBiddingManager sharedInstance] startWithRequestItem:request];
//}
//
//+ (void) sendWinnerNotifyWithCustomObject:(id)customObject secondPrice:(NSString*)price userInfo:(NSDictionary<NSString *, NSString *> *)userInfo {
//    NSLog(@"%s", __FUNCTION__);
//    CXHAdSDKSplashAd *splashAd = (CXHAdSDKSplashAd *)customObject;
//    [splashAd sendWinNotice:[price integerValue]];
//}
//
//+ (void)sendLossNotifyWithCustomObject:(nonnull id)customObject lossType:(ATBiddingLossType)lossType winPrice:(nonnull NSString *)price userInfo:(NSDictionary *)userInfo {
//    NSLog(@"%s", __FUNCTION__);
//    CXHAdSDKSplashAd *splashAd = (CXHAdSDKSplashAd *)customObject;
//    [splashAd sendLossNotice:[price integerValue]];
//}
//
//+ (void)initCXHSDKWith:(NSString*)appid domain:(NSString *)domain completion:(void (^)(void))completion {
//    NSLog(@"====== start init cxh sdk");
//    [CXHAdSDK initWithAppId:appid requestDomain:domain completionBlock:^(NSError * _Nullable error) {
//        if (!error && completion != nil) {
//            completion();
//        }
//    }];
//}

@end
